<?php

namespace app\api\controller;

use app\admin\model\Seller;
use app\common\controller\Api;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/13/013
 * Time: 17:44
 */
class Selleremployee extends Api
{
    protected $noNeedLogin = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 商家人员列表
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $seller_id = $this->request->request('seller_id');
        $seller = Seller::get($seller_id);
        if(!$seller){
            $this->error('没有这个商家');
        }
        $model = new \app\api\model\Selleremployee;
        $employeeList = $model->where('seller_id',$seller_id)->select();
        $this->success('',$employeeList);
    }

    /**
     * 添加配送员或者打包员
     */
    public function add()
    {
        $seller_id = $this->request->request('seller_id');
        $name = $this->request->request('name');
        $phone = $this->request->request('phone');
        $position = $this->request->request('position');
        $workTime = $this->request->request('worktime');
        $salary = $this->request->request('salary');
        //验证--
        if (!$seller_id || !$name || !$phone || !$position || !$workTime || !$salary) {
            $this->error(__('Invalid parameters'));
        }

        $sellerEmployee = new \app\api\model\Selleremployee($_POST);
        $result = $sellerEmployee->save();
        if ($result) {
            $this->success('新增人员');
        }
        $this->error('插入失败');
    }


    /**修改人员信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $id = $this->request->request('id');
        $seller_id = $this->request->request('seller_id');
        $name = $this->request->request('name');
        $phone = $this->request->request('phone');
        $position = $this->request->request('position');
        $workTime = $this->request->request('worktime');
        $salary = $this->request->request('salary');
        if (!$seller_id || !$name || !$phone || !$position || !$workTime || !$salary) {
            $this->error(__('Invalid parameters'));
        }
        $sellerEmployee = db('selleremployee')->where('id', $id)->find();
        if (!$sellerEmployee) {
            $this->error('没有这个人员');
        }

        $employee = new \app\api\model\Selleremployee();
        $result = $employee->allowField(true)->save($_POST, ['id' => $id]);
        if ($result) {
            $this->success('成功');
        }
        $this->error('失败');
    }

    /**
     * 删除人员
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $id = $this->request->request('id');
        $employee = \app\api\model\Selleremployee::get($id);
        if (!$employee) {
            $this->error('没有这个人员');
        }
        $result = $employee->delete();
        if($result){
            $this->success('成功');
        }
        $this->error('失败');
    }
}